package com.tanhua.manager.controller;

import com.tanhua.domain.db.Admin;
import com.tanhua.domain.vo.AdminVo;
import com.tanhua.manager.interceptors.AdminHolder;
import com.tanhua.manager.service.AdminService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/system/users")
public class SystemUsersController {

    @Autowired
    private AdminService adminService;

    @GetMapping(value = "/verification",name = "登录时获取验证码图片")
    public ResponseEntity verification(String uuid, HttpServletResponse response) throws Exception{
        return adminService.verification(uuid,response);
    }


    @PostMapping(value = "/login",name = "登录方法")
    public ResponseEntity login(@RequestBody Map param) throws Exception{
        String username = (String) param.get("username");
        String password = (String) param.get("password");
        String verificationCode = (String) param.get("verificationCode");
        String uuid = (String) param.get("uuid");
        return adminService.login(username,password,verificationCode,uuid);
    }

    @PostMapping(value = "/profile",name = "验证token，获取用户信息")
    public ResponseEntity profile() throws Exception{
        Admin admin = AdminHolder.getAdmin();

        AdminVo adminVo = new AdminVo();
        adminVo.setId(admin.getId());
        adminVo.setUsername(admin.getUsername());
        adminVo.setAvatar(admin.getAvatar());

        return ResponseEntity.ok(adminVo);
    }
}
